package com.idlecode.panel.tool;

import cn.hutool.core.util.StrUtil;
import com.idlecode.service.ToolService;
import com.idlecode.util.sm.MyKeyPair;
import com.idlecode.util.sm.SM2Util;
import com.idlecode.util.sm.SM3Util;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.NlsSafe;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-11-07 08:56
 **/
public class SMPanel extends JPanel {

    public SMPanel() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); // Vertical layout

        JPanel SM2Pane = createSM2CommonPanel();
        this.add(SM2Pane);
        JPanel SM3Pane = createSM3CommonPanel();
        this.add(SM3Pane);
        /****************添加空白行*****************/
        this.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)));
    }

    private JPanel createSM2CommonPanel() {
        JPanel SM2Pane = new JPanel();
        SM2Pane.setLayout(new BorderLayout());

        JPanel SM2TitlePanel = createTitlePanel("SM2");
        // 添加标题面板到主面板
        SM2Pane.add(SM2TitlePanel, BorderLayout.NORTH);

        JPanel SM2ContentPanel = new JPanel();
        SM2ContentPanel.setLayout(new BoxLayout(SM2ContentPanel, BoxLayout.Y_AXIS));

        /******************曲线模型选择**开始***********************/
        JPanel curveModelPanel = new JPanel();
        curveModelPanel.setLayout(new BoxLayout(curveModelPanel, BoxLayout.X_AXIS));
        curveModelPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        JLabel curveNameLabel = new JLabel("曲线模型: ");
        curveModelPanel.add(curveNameLabel);
        ComboBox<String> curveNameComboBox = new ComboBox<>();
        curveNameComboBox.setMinimumSize(new Dimension(0, 30));
        curveNameComboBox.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        curveModelPanel.add(curveNameComboBox);
        List<String> curveNames = SM2Util.getCurveNames();
        for (String curveName : curveNames) {
            curveNameComboBox.addItem(curveName);
        }
        String curveName = ToolService.getInstance().getValue("curveName");
        if (StrUtil.isNotBlank(curveName)) {
            curveNameComboBox.setSelectedItem(curveName);
        }
        curveNameComboBox.addActionListener(e -> {
            Object selectedItem = curveNameComboBox.getSelectedItem();
            if (selectedItem != null) {
                ToolService.getInstance().put("curveName", selectedItem.toString());
            }
        });
        SM2ContentPanel.add(curveModelPanel);
        /******************曲线模型选择**结束***********************/

        /****************添加空白行*****************/
        SM2ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /*********************密钥对生成**开始********************/
        JPanel sm2PublicKeyPanel = new JPanel();
        sm2PublicKeyPanel.setLayout(new BoxLayout(sm2PublicKeyPanel, BoxLayout.X_AXIS));
        sm2PublicKeyPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel sm2PublicKeyLabel = new JLabel("公钥: ");
        sm2PublicKeyPanel.add(sm2PublicKeyLabel);
        JTextField sm2PublicKeyTField = new JTextField();
        sm2PublicKeyTField.setMinimumSize(new Dimension(0, 30));
        sm2PublicKeyTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2PublicKeyPanel.add(sm2PublicKeyTField);
        SM2ContentPanel.add(sm2PublicKeyPanel);

        JPanel sm2PrivateKeyPanel = new JPanel();
        sm2PrivateKeyPanel.setLayout(new BoxLayout(sm2PrivateKeyPanel, BoxLayout.X_AXIS));
        sm2PrivateKeyPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel sm2PrivateKeyLabel = new JLabel("私钥: ");
        sm2PrivateKeyPanel.add(sm2PrivateKeyLabel);
        JTextField sm2PrivateKeyTField = new JTextField();
        sm2PrivateKeyTField.setMinimumSize(new Dimension(0, 30));
        sm2PrivateKeyTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2PrivateKeyPanel.add(sm2PrivateKeyTField);
        JButton genPubPriKeyButton = new JButton("生成");
        sm2PrivateKeyPanel.add(genPubPriKeyButton);
        genPubPriKeyButton.addActionListener(e -> {
            Object selectedItem = curveNameComboBox.getSelectedItem();
            if (selectedItem == null) {
                Messages.showMessageDialog("请选择曲线名称！", "警告", Messages.getWarningIcon());
                return;
            }
            SM2Util sm2Util = new SM2Util(selectedItem.toString());
            MyKeyPair myKeyPair = sm2Util.genKeyPair();
            sm2PublicKeyTField.setText(myKeyPair.publicKey());
            sm2PrivateKeyTField.setText(myKeyPair.privateKey());
        });
        SM2ContentPanel.add(sm2PrivateKeyPanel);
        /*********************密钥对生成**结束********************/

        /****************添加空白行*****************/
        SM2ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /********************解密**开始********************/
        JPanel sm2DecryptTextPanel = new JPanel();
        sm2DecryptTextPanel.setLayout(new BoxLayout(sm2DecryptTextPanel, BoxLayout.X_AXIS));
        sm2DecryptTextPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        JLabel sm2DecryptTextLabel = new JLabel("加密文本: ");
        sm2DecryptTextPanel.add(sm2DecryptTextLabel);
        JTextField sm2DecryptTextTField = new JTextField();
        sm2DecryptTextTField.setMinimumSize(new Dimension(0, 30));
        sm2DecryptTextTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2DecryptTextPanel.add(sm2DecryptTextTField);
        SM2ContentPanel.add(sm2DecryptTextPanel);

        JPanel sm2DecryptPrivateKeyPanel = new JPanel();
        sm2DecryptPrivateKeyPanel.setLayout(new BoxLayout(sm2DecryptPrivateKeyPanel, BoxLayout.X_AXIS));
        sm2DecryptPrivateKeyPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel sm2DecryptPrivateKeyLabel = new JLabel("私钥: ");
        sm2DecryptPrivateKeyPanel.add(sm2DecryptPrivateKeyLabel);
        JTextField sm2DecryptPrivateKeyTField = new JTextField();
        sm2DecryptPrivateKeyTField.setMinimumSize(new Dimension(0, 30));
        sm2DecryptPrivateKeyTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        String SM2PrivateKey = ToolService.getInstance().getValue("SM2PrivateKey");
        if (StrUtil.isNotBlank(SM2PrivateKey)) {
            sm2DecryptPrivateKeyTField.setText(SM2PrivateKey);
        }
        sm2DecryptPrivateKeyPanel.add(sm2DecryptPrivateKeyTField);
        SM2ContentPanel.add(sm2DecryptPrivateKeyPanel);

        JPanel sm2DecryptResultPanel = new JPanel();
        sm2DecryptResultPanel.setLayout(new BoxLayout(sm2DecryptResultPanel, BoxLayout.X_AXIS));
        sm2DecryptResultPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        JLabel sm2DecryptResultLabel = new JLabel("解密文本: ");
        sm2DecryptResultPanel.add(sm2DecryptResultLabel);
        JTextField sm2DecryptResultTField = new JTextField();
        sm2DecryptResultTField.setMinimumSize(new Dimension(0, 30));
        sm2DecryptResultTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2DecryptResultPanel.add(sm2DecryptResultTField);
        JButton sm2DecryptButton = new JButton("解密");
        sm2DecryptResultPanel.add(sm2DecryptButton);
        sm2DecryptButton.addActionListener(e -> {
            Object selectedItem = curveNameComboBox.getSelectedItem();
            if (selectedItem == null) {
                Messages.showMessageDialog("请选择曲线名称！", "警告", Messages.getWarningIcon());
                return;
            }
            SM2Util sm2Util = new SM2Util(selectedItem.toString());
            //加密文本
            String encryptStr = sm2DecryptTextTField.getText();
            if (StrUtil.isBlank(encryptStr)) {
                Messages.showMessageDialog("加密文本不能为空！", "警告", Messages.getWarningIcon());
                return;
            }

            String privateKey = sm2DecryptPrivateKeyTField.getText();
            if (StrUtil.isBlank(privateKey)) {
                Messages.showMessageDialog("私钥不能为空！", "警告", Messages.getWarningIcon());
                return;
            }
            //保存私钥
            ToolService.getInstance().put("SM2PrivateKey", privateKey);
            try {
                //解密
                String decryptStr = sm2Util.decrypt(encryptStr, privateKey);
                sm2DecryptResultTField.setText(decryptStr);
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        SM2ContentPanel.add(sm2DecryptResultPanel);
        /********************解密**结束********************/

        /****************添加空白行*****************/
        SM2ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /********************解密**开始********************/
        JPanel sm2EncryptTextPanel = new JPanel();
        sm2EncryptTextPanel.setLayout(new BoxLayout(sm2EncryptTextPanel, BoxLayout.X_AXIS));
        sm2EncryptTextPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel sm2EncryptTextLabel = new JLabel("文本: ");
        sm2EncryptTextPanel.add(sm2EncryptTextLabel);
        JTextField sm2EncryptTextTField = new JTextField();
        sm2EncryptTextTField.setMinimumSize(new Dimension(0, 30));
        sm2EncryptTextTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2EncryptTextPanel.add(sm2EncryptTextTField);
        SM2ContentPanel.add(sm2EncryptTextPanel);

        JPanel sm2EncryptPublicKeyPanel = new JPanel();
        sm2EncryptPublicKeyPanel.setLayout(new BoxLayout(sm2EncryptPublicKeyPanel, BoxLayout.X_AXIS));
        sm2EncryptPublicKeyPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel sm2EncryptPublicKeyLabel = new JLabel("公钥: ");
        sm2EncryptPublicKeyPanel.add(sm2EncryptPublicKeyLabel);
        JTextField sm2EncryptPublicKeyTField = new JTextField();
        sm2EncryptPublicKeyTField.setMinimumSize(new Dimension(0, 30));
        sm2EncryptPublicKeyTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2EncryptPublicKeyPanel.add(sm2EncryptPublicKeyTField);
        String SM2PublicKey = ToolService.getInstance().getValue("SM2PublicKey");
        if (StrUtil.isNotBlank(SM2PublicKey)) {
            sm2EncryptPublicKeyTField.setText(SM2PublicKey);
        }
        SM2ContentPanel.add(sm2EncryptPublicKeyPanel);

        JPanel sm2EncryptResultPanel = new JPanel();
        sm2EncryptResultPanel.setLayout(new BoxLayout(sm2EncryptResultPanel, BoxLayout.X_AXIS));
        sm2EncryptResultPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        JLabel sm2EncryptResultLabel = new JLabel("加密文本: ");
        sm2EncryptResultPanel.add(sm2EncryptResultLabel);
        JTextField sm2EncryptResultTField = new JTextField();
        sm2EncryptResultTField.setMinimumSize(new Dimension(0, 30));
        sm2EncryptResultTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        sm2EncryptResultPanel.add(sm2EncryptResultTField);
        JButton sm2EncryptButton = new JButton("加密");
        sm2EncryptResultPanel.add(sm2EncryptButton);
        sm2EncryptButton.addActionListener(e -> {
            Object selectedItem = curveNameComboBox.getSelectedItem();
            if (selectedItem == null) {
                Messages.showMessageDialog("请选择曲线名称！", "警告", Messages.getWarningIcon());
                return;
            }
            SM2Util sm2Util = new SM2Util(selectedItem.toString());
            //文本内容
            String encryptText = sm2EncryptTextTField.getText();
            if (StrUtil.isBlank(encryptText)) {
                Messages.showMessageDialog("文本不能为空！", "警告", Messages.getWarningIcon());
                return;
            }
            String publicKey = sm2EncryptPublicKeyTField.getText();

            if (StrUtil.isBlank(publicKey)) {
                Messages.showMessageDialog("公钥不能为空！", "警告", Messages.getWarningIcon());
                return;
            }
            //保存公钥
            ToolService.getInstance().put("SM2PublicKey", publicKey);
            try {
                String encryptStr = sm2Util.encrypt(encryptText, publicKey);
                sm2EncryptResultTField.setText(encryptStr);
            } catch (Exception ex) {
                Messages.showMessageDialog(ex.getMessage(), "错误", Messages.getErrorIcon());
            }
        });
        SM2ContentPanel.add(sm2EncryptResultPanel);
        /********************解密**结束********************/

        /****************添加空白行*****************/
        SM2ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        SM2Pane.add(SM2ContentPanel, BorderLayout.CENTER);
        return SM2Pane;
    }

    private JPanel sourceNamePanel;

    private JLabel sourceNameLabel;

    private JScrollPane sourceTextPane;

    private TextFieldWithBrowseButton fileChooser;

    private JTextField resultTField;

    private JPanel createSM3CommonPanel() {
        JPanel SM3Pane = new JPanel();
        SM3Pane.setLayout(new BorderLayout());

        JPanel SM3TitlePanel = createTitlePanel("SM3");
        // 添加标题面板到主面板
        SM3Pane.add(SM3TitlePanel, BorderLayout.NORTH);

        JPanel SM3ContentPanel = new JPanel(new GridBagLayout());
        SM3ContentPanel.setLayout(new BoxLayout(SM3ContentPanel, BoxLayout.Y_AXIS));

        /****************添加空白行*****************/
        SM3ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /******************文本or文件*开始***********************/
        JPanel sourceTypePanel = new JPanel();
        sourceTypePanel.setLayout(new BoxLayout(sourceTypePanel, BoxLayout.X_AXIS));
        sourceTypePanel.add(Box.createHorizontalGlue()); // 左侧弹性空间
        JRadioButton textJRadioButton = new JRadioButton("文本", false);
        textJRadioButton.addActionListener(e -> {
            sourceNameLabel.setText("文本: ");
            ToolService.getInstance().put("SM3SourceType", "text");
            sourceNamePanel.remove(fileChooser);
            sourceNamePanel.add(sourceTextPane);
            sourceNamePanel.revalidate();
            sourceNamePanel.repaint();
            resultTField.setText(null);
        });
        sourceTypePanel.add(textJRadioButton);

        sourceTypePanel.add(Box.createRigidArea(new Dimension(10, 0)));

        JRadioButton fileJRadioButton = new JRadioButton("文件", false);
        fileJRadioButton.addActionListener(e -> {
            sourceNameLabel.setText("文件: ");
            ToolService.getInstance().put("SM3SourceType", "file");
            sourceNamePanel.remove(sourceTextPane);
            sourceNamePanel.add(fileChooser);
            sourceNamePanel.revalidate();
            sourceNamePanel.repaint();
            resultTField.setText(null);
        });
        sourceTypePanel.add(fileJRadioButton);
        sourceTypePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间

        ButtonGroup sourceTypeGroup = new ButtonGroup();
        sourceTypeGroup.add(textJRadioButton);
        sourceTypeGroup.add(fileJRadioButton);

        String SM3SourceType = ToolService.getInstance().getValue("SM3SourceType");
        if (StrUtil.isNotBlank(SM3SourceType)) {
            if ("text".equals(SM3SourceType)) {
                textJRadioButton.setSelected(true);
            } else if ("file".equals(SM3SourceType)) {
                fileJRadioButton.setSelected(true);
            } else {
                textJRadioButton.setSelected(true);
            }
        } else {
            textJRadioButton.setSelected(true);
        }
        SM3ContentPanel.add(sourceTypePanel);
        /******************文本or文件*结束***********************/

        /****************添加空白行*****************/
        SM3ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /******************文本or文件输入*结束***********************/
        sourceNamePanel = new JPanel();
        sourceNamePanel.setLayout(new BoxLayout(sourceNamePanel, BoxLayout.X_AXIS));
        sourceNamePanel.add(Box.createRigidArea(new Dimension(29, 0)));
        if (textJRadioButton.isSelected()) {
            sourceNameLabel = new JLabel("文本: ");
        } else {
            sourceNameLabel = new JLabel("文件: ");
        }
        sourceNamePanel.add(sourceNameLabel);

        JTextArea sourceTextArea = new JTextArea();
        sourceTextPane = new JScrollPane(sourceTextArea);
        sourceTextPane.setMinimumSize(new Dimension(0, 150));
        sourceTextPane.setMaximumSize(new Dimension(Integer.MAX_VALUE, 150));
        sourceTextPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        sourceTextPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        // 创建一个带有浏览按钮的文本框
        fileChooser = new TextFieldWithBrowseButton();
        fileChooser.setMinimumSize(new Dimension(0, 30));
        fileChooser.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        // 配置文件选择描述符，用于控制用户可以选择的文件类型
        FileChooserDescriptor fileDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(); // 只允许选择文件
        // 添加浏览按钮监听器，当用户选择文件后触发事件
        fileChooser.addBrowseFolderListener(
                "选择文件",  // 弹窗标题
                null,        // 提示文本
                null,        // 项目上下文（可为空）
                fileDescriptor,
                new TextComponentAccessor<>() {
                    @Override
                    public @NlsSafe String getText(JTextField jTextField) {
                        return jTextField.getText();
                    }

                    @Override
                    public void setText(JTextField jTextField, @NlsSafe @NotNull String s) {
                        jTextField.setText(s);
                        resultTField.setText(null);
                    }
                }
        );

        if (textJRadioButton.isSelected()) {
            sourceNamePanel.add(sourceTextPane);
        } else {
            sourceNamePanel.add(fileChooser);
        }

        sourceNamePanel.revalidate();
        sourceNamePanel.repaint();
        SM3ContentPanel.add(sourceNamePanel);
        /******************文本or文件输入*结束***********************/

        /****************添加空白行*****************/
        SM3ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /******************输出类型*开始***********************/
        JPanel outPutTypePanel = new JPanel();
        outPutTypePanel.setLayout(new BoxLayout(outPutTypePanel, BoxLayout.X_AXIS));
        outPutTypePanel.add(Box.createHorizontalGlue()); // 左侧弹性空间
        JRadioButton base64JRadioButton = new JRadioButton("Base64", true);
        base64JRadioButton.addActionListener(e -> {
            ToolService.getInstance().put("SM3OutPutType", "Base64");
            resultTField.setText(null);
        });
        outPutTypePanel.add(base64JRadioButton);

        outPutTypePanel.add(Box.createRigidArea(new Dimension(10, 0)));

        JRadioButton hexJRadioButton = new JRadioButton("Hex", false);
        hexJRadioButton.addActionListener(e -> {
            ToolService.getInstance().put("SM3OutPutType", "Hex");
            resultTField.setText(null);
        });
        outPutTypePanel.add(hexJRadioButton);
        outPutTypePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间

        ButtonGroup outPutTypeGroup = new ButtonGroup();
        outPutTypeGroup.add(base64JRadioButton);
        outPutTypeGroup.add(hexJRadioButton);

        String SM3OutPutType = ToolService.getInstance().getValue("SM3OutPutType");
        if (StrUtil.isNotBlank(SM3OutPutType)) {
            if ("Base64".equals(SM3OutPutType)) {
                base64JRadioButton.setSelected(true);
            } else if ("Hex".equals(SM3OutPutType)) {
                hexJRadioButton.setSelected(true);
            } else {
                base64JRadioButton.setSelected(true);
            }
        } else {
            base64JRadioButton.setSelected(true);
        }
        SM3ContentPanel.add(outPutTypePanel);
        /******************输出类型*结束***********************/

        /****************添加空白行*****************/
        SM3ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        /******************输出结果*开始***********************/
        JPanel resultPanel = new JPanel();
        resultPanel.setLayout(new BoxLayout(resultPanel, BoxLayout.X_AXIS));
        resultPanel.add(Box.createRigidArea(new Dimension(29, 0)));
        JLabel resultLabel = new JLabel("结果: ");
        resultPanel.add(resultLabel);

        resultTField = new JTextField();
        resultTField.setMinimumSize(new Dimension(0, 30));
        resultTField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        resultPanel.add(resultTField);

        JButton operationButton = new JButton("运算");
        resultPanel.add(operationButton);
        operationButton.addActionListener(e -> {
            if (textJRadioButton.isSelected()) {
                String text = sourceTextArea.getText();
                if (StrUtil.isBlank(text)) {
                    return;
                }
                if (base64JRadioButton.isSelected()) {
                    String hashToBase64 = SM3Util.hashToBase64(text);
                    resultTField.setText(hashToBase64);
                } else {
                    String hashToHex = SM3Util.hashToHex(text);
                    resultTField.setText(hashToHex);
                }
            } else {
                String chooserText = fileChooser.getText();
                if (StrUtil.isBlank(chooserText)) {
                    return;
                }
                File file = new File(chooserText);
                if (file.isDirectory()) {
                    Messages.showMessageDialog("请选择文件！", "警告", Messages.getWarningIcon());
                    return;
                }
                if (base64JRadioButton.isSelected()) {
                    try {
                        String hashFileToBase64 = SM3Util.hashFileToBase64(file);
                        resultTField.setText(hashFileToBase64);
                    } catch (IOException ex) {
                        Messages.showMessageDialog(ex.getMessage(), "异常", Messages.getErrorIcon());
                    }
                } else {
                    try {
                        String hashFileToHex = SM3Util.hashFileToHex(file);
                        resultTField.setText(hashFileToHex);
                    } catch (IOException ex) {
                        Messages.showMessageDialog(ex.getMessage(), "异常", Messages.getErrorIcon());
                    }
                }
            }
        });
        SM3ContentPanel.add(resultPanel);
        /******************输出结果*结束***********************/

        /****************添加空白行*****************/
        SM3ContentPanel.add(Box.createRigidArea(new Dimension(Integer.MAX_VALUE, 15)));

        SM3Pane.add(SM3ContentPanel, BorderLayout.CENTER);

        return SM3Pane;
    }

    private JPanel createTitlePanel(String title) {
        // 创建带横线的标题
        JPanel titlePanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 创建标题标签
        JLabel titleLabel = new JLabel(title + " ");
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 12));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));

        // 添加标题标签
        gbc.gridx = 0;
        gbc.weightx = 0; // 不占用额外空间
        gbc.anchor = GridBagConstraints.CENTER; // 垂直居中
        titlePanel.add(titleLabel, gbc);

        // 创建分隔符
        JSeparator separator = new JSeparator();
        separator.setForeground(Color.GRAY);

        // 添加分隔符
        gbc.gridx = 1;
        gbc.weightx = 1; // 占用剩余空间
        gbc.anchor = GridBagConstraints.CENTER; // 垂直居中
        titlePanel.add(separator, gbc);
        return titlePanel;
    }
}
